package com.xyht.sca_s.student_manage_system.modules.asset.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.req.SmsAssetMaterialProcurementApplyAllowReq;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.req.SmsAssetMaterialProcurementApplyReq;
import com.xyht.sca_s.student_manage_system.modules.asset.service.SmsAssetMaterialProcurementApplyService;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.entity.req.consumableInStorageApply.SmsAssetConsumableInStorageApplyAllowReq;
import com.xyht.sca_s.student_manage_system.modules.assetConsumable.entity.req.consumableInStorageApply.SmsAssetConsumableInStorageApplyReq;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 * 物资采购申请单 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2024-09-19
 */
@RestController
@RequestMapping("/asset/materialProcurement")
@SecretBody
public class SmsAssetMaterialProcurementApplyController {


    @Resource
    private SmsAssetMaterialProcurementApplyService smsAssetMaterialProcurementApplyService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation(value = "提交物资采购申请")
    @PostMapping("/add")
    public ResponseResult addMaterialProcurementApply(@RequestBody SmsAssetMaterialProcurementApplyReq req,HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetMaterialProcurementApplyService.addMaterialProcurementApply(req, userId);
    }

    @ApiOperation(value = "撤销物资采购申请")
    @PostMapping("/cancel")
    public ResponseResult cancelMaterialProcurementApply(@RequestBody SmsAssetMaterialProcurementApplyReq cancelReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetMaterialProcurementApplyService.cancelMaterialProcurementApply(cancelReq, userId);
    }

    @ApiOperation(value = "审批列表")
    @GetMapping("/list")
    public ResponseResult getMaterialProcurementApplyList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                          @RequestParam(value = "applyType") Integer applyType,
                                                          HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetMaterialProcurementApplyService.getMaterialProcurementApplyList(userId, applyType, pageNum, pageSize);
    }

    @ApiOperation(value = "审批详情")
    @GetMapping("/detail")
    public ResponseResult getMaterialProcurementApplyDetail(@RequestParam(value = "applyId") String applyId, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetMaterialProcurementApplyService.getMaterialProcurementApplyDetail(applyId, userId);
    }

    @ApiOperation(value = "审批")
    @PostMapping("/allow")
    public ResponseResult allowMaterialProcurementApply(@RequestBody SmsAssetMaterialProcurementApplyAllowReq allowReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetMaterialProcurementApplyService.allowMaterialProcurementApply(allowReq, userId);
    }

    @ApiOperation(value = "表格下载")
    @GetMapping("/download")
    public ResponseResult tableDownload(@RequestParam("applyId") String applyId, HttpServletResponse response) {
        ResponseResult responseResult;
        try {
            Future<ResponseResult> future = smsAssetMaterialProcurementApplyService.tableDownload(applyId, response);
            while (true) {
                if (future.isDone()) {
                    break;
                }
            }
            responseResult = future.get();
        } catch (Exception e) {
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiModelProperty(value = "获取所有审批列表")
    @GetMapping("/allList")
    public ResponseResult getMaterialProcurementApplyAllList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestParam(value = "allowStatus", required = false) Integer allowStatus,
                                                             @RequestParam(value = "applyName", required = false) String applyName) {
        return smsAssetMaterialProcurementApplyService.getMaterialProcurementApplyAllList(allowStatus,applyName ,pageNum, pageSize);
    }
}

